TLVIVI.Pacman.Map = function(domElementId) {
	this._mapElem = $("#" + domElementId).get(0);
	this._apples = null;
	this._currPlayers = null;
}

TLVIVI.Pacman.Map.prototype.initMap = function(lat, lng, zoomInfo) {
	var latlng = new google.maps.LatLng(lat, lng);
    
    var myOptions = {
      zoom: 16,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.SATELLITE 
    };

    this._map = new google.maps.Map(this._mapElem, myOptions);
    this._apples = new Hashtable();
    this._currPlayers = new Hashtable();
}

TLVIVI.Pacman.Map.prototype.drawGameBorder = function(borderPoints) {
	var points = [];
	
	$.each(borderPoints, function(index, value){
		points[index] = new google.maps.LatLng(value.Lat, value.Lon);
	});
	
	var gameBorder = new google.maps.Polygon({
	    paths: points,
	    strokeColor: "#FF0000",
	    strokeOpacity: 0.8,
	    strokeWeight: 2,
	    fillColor: "#FF0000",
	    fillOpacity: 0.35
	  });

	gameBorder.setMap(this._map);
}

TLVIVI.Pacman.Map.prototype.addApple = function(lat, lng, id, kind) {
	var latlng = new google.maps.LatLng(lat, lng);
	
	var applePic = "/images/apple.gif";
	if (kind == 2){
		applePic = "/images/cherry.gif";
	}
	else if (kind == 1){
		applePic == "/images/superApple.gif";
	}
	
	var appleMarker = new google.maps.Marker({
	      position: latlng,
	      map: this._map,
	      icon: applePic
	  });
	
	this._apples.put(id, appleMarker);
}

TLVIVI.Pacman.Map.prototype.clearAllApples = function() {
	$.each(this._apples.values(), function(index, value){
		value.setMap(null);
	});
	
	this._apples = new Hashtable();
}

TLVIVI.Pacman.Map.prototype.drawAllApples = function(apples) {
	this.clearAllApples();
	var self = this;
	apples.each(function(key, value){
		self.addApple(value.Lat, value.Lng, value.EntityId, value.Kind);
	});
}

TLVIVI.Pacman.Map.prototype.updateRemovedApples = function(removedApples){
	var self = this;
	$.each(removedApples, function(index, value){
		self._apples.get(value).setMap(null);
		self._apples.remove(value);
	})
}

TLVIVI.Pacman.Map.prototype.addPlayer = function(playerName, playerId, role, status, lat, lng){
	var latlng = new google.maps.LatLng(lat, lng);
	var playerMarker;
	if (role == 1){
		playerMarker = new google.maps.Marker({
		      position: latlng,
		      map: this._map,
		      icon: "/images/pacman.gif"
		  });
		
		this._currPlayers.put(playerId, playerMarker);
	}
	else if (role == 2){
		var monsterIcon;
		if (status == 5){
			monsterIcon = "/images/monsterSuperMode.gif"
		}
		else {
			monsterIcon = "/images/monster.gif";
		}
		
		playerMarker = new google.maps.Marker({
		      position: latlng,
		      map: this._map,
		      icon: monsterIcon
		  });
		
		this._currPlayers.put(playerId, playerMarker);
	}
}
	
TLVIVI.Pacman.Map.prototype.clearAllPlayers = function(){
	$.each(this._currPlayers.values(), function(index, value){
		value.setMap(null);
	});
	
	this._currPlayers = new Hashtable();
}

TLVIVI.Pacman.Map.prototype.drawPlayers = function(players, gameState){
	this.clearAllPlayers();
	
	for (var index = 0; index < players.length; index++) {
		this.addPlayer(players[index].PlayerName, players[index].EntityId, players[index].Role, gameState, players[index].Lat, players[index].Lng);
	}
}